package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.CureMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.CureTypeDAO;

import cn.edu.scau.cmi.hombio.domain.CureMedicine;
import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.CureType;

import cn.edu.scau.cmi.hombio.service.CureMethodService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for CureMethod entities
 * 
 */

@Controller("CureMethodRestController")

public class CureMethodRestController {

	/**
	 * DAO injected by Spring that manages CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineDAO cureMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages CureType entities
	 * 
	 */
	@Autowired
	private CureTypeDAO cureTypeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodService cureMethodService;

	/**
	 * Save an existing CureType entity
	 * 
	 */
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureType", method = RequestMethod.PUT)
	@ResponseBody
	public CureType saveCureMethodCureType(@PathVariable Integer curemethod_id, @RequestBody CureType curetype) {
		cureMethodService.saveCureMethodCureType(curemethod_id, curetype);
		return cureTypeDAO.findCureTypeByPrimaryKey(curetype.getId());
	}

	/**
	* View an existing CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureTaskCourse/{curetaskcourse_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureTaskCourse loadCureMethodCureTaskCourse(@PathVariable Integer curemethod_id, @PathVariable Integer related_curetaskcourse_id) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourse_id, -1, -1);

		return curetaskcourse;
	}

	/**
	* Delete an existing CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMethod(@PathVariable Integer curemethod_id) {
		CureMethod curemethod = cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id);
		cureMethodService.deleteCureMethod(curemethod);
	}

	/**
	* Show all CureMethod entities
	* 
	*/
	@RequestMapping(value = "/CureMethod", method = RequestMethod.GET)
	@ResponseBody
	public List<CureMethod> listCureMethods() {
		return new java.util.ArrayList<CureMethod>(cureMethodService.loadCureMethods());
	}

	/**
	* Create a new CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMethod", method = RequestMethod.POST)
	@ResponseBody
	public CureMethod newCureMethod(@RequestBody CureMethod curemethod) {
		cureMethodService.saveCureMethod(curemethod);
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod.getId());
	}

	/**
	* Save an existing CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureTaskCourse", method = RequestMethod.PUT)
	@ResponseBody
	public CureTaskCourse saveCureMethodCureTaskCourse(@PathVariable Integer curemethod_id, @RequestBody CureTaskCourse curetaskcourse) {
		cureMethodService.saveCureMethodCureTaskCourse(curemethod_id, curetaskcourse);
		return cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse.getId());
	}

	/**
	* Select an existing CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureMethod loadCureMethod(@PathVariable Integer curemethod_id) {
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id);
	}

	/**
	* View an existing CureType entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureType/{curetype_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureType loadCureMethodCureType(@PathVariable Integer curemethod_id, @PathVariable Integer related_curetype_id) {
		CureType curetype = cureTypeDAO.findCureTypeByPrimaryKey(related_curetype_id, -1, -1);

		return curetype;
	}

	/**
	* Delete an existing CureType entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureType/{curetype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMethodCureType(@PathVariable Integer curemethod_id, @PathVariable Integer related_curetype_id) {
		cureMethodService.deleteCureMethodCureType(curemethod_id, related_curetype_id);
	}

	/**
	* Create a new CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureTaskCourse", method = RequestMethod.POST)
	@ResponseBody
	public CureTaskCourse newCureMethodCureTaskCourse(@PathVariable Integer curemethod_id, @RequestBody CureTaskCourse curetaskcourse) {
		cureMethodService.saveCureMethodCureTaskCourse(curemethod_id, curetaskcourse);
		return cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse.getId());
	}

	/**
	* Delete an existing CureTaskCourse entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureTaskCourse/{curetaskcourse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMethodCureTaskCourse(@PathVariable Integer curemethod_id, @PathVariable Integer related_curetaskcourse_id) {
		cureMethodService.deleteCureMethodCureTaskCourse(curemethod_id, related_curetaskcourse_id);
	}

	/**
	* View an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureMedicines/{curemedicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureMedicine loadCureMethodCureMedicines(@PathVariable Integer curemethod_id, @PathVariable Integer related_curemedicines_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(related_curemedicines_id, -1, -1);

		return curemedicine;
	}

	/**
	* Save an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureMedicines", method = RequestMethod.PUT)
	@ResponseBody
	public CureMedicine saveCureMethodCureMedicines(@PathVariable Integer curemethod_id, @RequestBody CureMedicine curemedicines) {
		cureMethodService.saveCureMethodCureMedicines(curemethod_id, curemedicines);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicines.getId());
	}

	/**
	* Create a new CureType entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureType", method = RequestMethod.POST)
	@ResponseBody
	public CureType newCureMethodCureType(@PathVariable Integer curemethod_id, @RequestBody CureType curetype) {
		cureMethodService.saveCureMethodCureType(curemethod_id, curetype);
		return cureTypeDAO.findCureTypeByPrimaryKey(curetype.getId());
	}

	/**
	* Delete an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureMedicines/{curemedicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteCureMethodCureMedicines(@PathVariable Integer curemethod_id, @PathVariable Integer related_curemedicines_id) {
		cureMethodService.deleteCureMethodCureMedicines(curemethod_id, related_curemedicines_id);
	}

	/**
	* Save an existing CureMethod entity
	* 
	*/
	@RequestMapping(value = "/CureMethod", method = RequestMethod.PUT)
	@ResponseBody
	public CureMethod saveCureMethod(@RequestBody CureMethod curemethod) {
		cureMethodService.saveCureMethod(curemethod);
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod.getId());
	}

	/**
	* Get CureType entity by CureMethod
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureType", method = RequestMethod.GET)
	@ResponseBody
	public CureType getCureMethodCureType(@PathVariable Integer curemethod_id) {
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id).getCureType();
	}

	/**
	* Show all CureMedicine entities by CureMethod
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureMedicines", method = RequestMethod.GET)
	@ResponseBody
	public List<CureMedicine> getCureMethodCureMedicines(@PathVariable Integer curemethod_id) {
		return new java.util.ArrayList<CureMedicine>(cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id).getCureMedicines());
	}

	/**
	* Get CureTaskCourse entity by CureMethod
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureTaskCourse", method = RequestMethod.GET)
	@ResponseBody
	public CureTaskCourse getCureMethodCureTaskCourse(@PathVariable Integer curemethod_id) {
		return cureMethodDAO.findCureMethodByPrimaryKey(curemethod_id).getCureTaskCourse();
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/CureMethod/{curemethod_id}/cureMedicines", method = RequestMethod.POST)
	@ResponseBody
	public CureMedicine newCureMethodCureMedicines(@PathVariable Integer curemethod_id, @RequestBody CureMedicine curemedicine) {
		cureMethodService.saveCureMethodCureMedicines(curemethod_id, curemedicine);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine.getId());
	}
}